package de.fusionfactory.corpora

import com.google.common.io.Files
import com.google.common.base.Charsets

/**
 * Created by Markus Ackermann.
 * No rights reserved. 
 */
class LeipzigCorpusTest extends GroovyTestCase {
    final String metaFileName = 'cat_wikipedia_2007_100K-meta.txt'
    final String coocFileName = 'fra_wikipedia_2007_100K-co_s.txt'
    final String coocMetaFileName = 'fra_wikipedia_2007_100K-meta.txt'
    final String mockFileContents =  "1\tfontfile\tarialuni.ttf\n" +
                                     "2\tfontname\tArial Unicode MS\n" +
                                     "3\tdatabase encoding\tutf-8\n" +
                                     "4\tdatabase communication encoding\tutf-8\n" +
                                     "5\tcontent encoding\tutf-8\n" +
                                     "6\tbrowser version\t1.0\n" +
                                     "7\tbuild release\tMedusa-1.4-B774\n" +
                                     "8\tbuild date\t2009-06-09"

    File metaFile, coocFile, coocMetaFile

    void setUp() {
        def tempFile = File.createTempFile('doesnt', 'matter')
        metaFile = new File(tempFile.parent, metaFileName)
        coocFile = new File(tempFile.parent, coocFileName)
        coocMetaFile = new File(tempFile.parent, coocMetaFileName)
        [metaFile, coocMetaFile].each {
            Files.touch(it)
            it.write mockFileContents
        }
        tempFile.delete()
    }

    void testFromFile() {
        LeipzigCorpus corpus = LeipzigCorpus.fromFile(metaFile)
        assert corpus.languageId == 'cat'
        assert corpus.sourceType == 'wikipedia'
        assert corpus.timeDesc == '2007'
        assert corpus.sizeDesc == '100K'
        assert corpus.contentType == 'meta'
        assert corpus.metaFile.path ==~ /.*-meta.txt/
        assert corpus.encodingString == 'utf-8'
        assert corpus.encoding == Charsets.UTF_8

        corpus = LeipzigCorpus.fromFile(coocFile)
        assert corpus.languageId == 'fra'
        assert corpus.sourceType == 'wikipedia'
        assert corpus.timeDesc == '2007'
        assert corpus.sizeDesc == '100K'
        assert corpus.contentType == 'co_s'
        assert corpus.metaFile.path ==~ /.*-meta.txt/
        assert corpus.encodingString == 'utf-8'
        assert corpus.encoding == Charsets.UTF_8
    }

    void tearDown() {
        [metaFile, coocFile].each { it.delete() }
    }
}
